package ui.magic.magiclibs.container.View;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.Button;

import java.util.Vector;

import ui.magic.magiclibs.container.Linear.LinearMagicUi;
import ui.magic.magiclibs.container.Tool.TimerObserver;
import ui.magic.magiclibs.container.Tool.Timer;
import ui.magic.magiclibs.container.View.Observer.ChangeItemObserver;

/**
 * Created by liushiyin on 2017/10/22.
 */

public class ChangeItemBar extends LinearMagicUi implements TimerObserver {

    private Timer timer;
    private int curSelectIndex;
    private int itemSize=3;
    private int itemColor;
    private Vector<Button> items;
    private ChangeItemObserver observer=null;

    public ChangeItemBar(Context context) {
        super(context);
        init();
    }

    public ChangeItemBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChangeItemBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        timer.exitTimer();
    }

    private void init(){
        timer=new Timer();
        this.setHorizontal();
        timer.setTimerTimerObserver(this);
        timer.addTimer(1,3000,"");
        this.setAlign(LinearMagicUi.CENTER);
        items=new Vector<>();
        Button item=new Button(getContext());
        itemColor=0xFFFF5E00;
        item.setBackgroundColor(itemColor);
        items.add(item);
        curSelectIndex=0;
        this.addChild(item,itemSize,70);
        this.setChildMargin(item,10,10,10,10);
    }
    public void setItems(int nums){
        int size=items.size();
        if(size<nums){
            for(int i=0;i<nums-size;++i){
                Button item=new Button(getContext());
                item.setBackgroundColor(itemColor);
                items.add(item);
                this.addChild(item,itemSize,70);
                this.setChildMargin(item,10,10,10,10);
            }
        }else if(size>nums){
            for(int i=0;i<size-nums;++i){
                this.removeChild(items.get(0));
                items.remove(0);
            }
        }
        curSelectIndex=0;
        items.get(curSelectIndex).setScaleX(1.5f);
        items.get(curSelectIndex).setScaleY(1.5f);
    }

    public void setItemColor(int color){
        itemColor=color;
        for(int i=0;i<items.size();++i){
            items.get(i).setBackgroundColor(itemColor);
        }
    }

    @Override
    public void OnTimer(int id, String describe) {
        switch (id){
            case 1:
            {
                changeNextItem();
            }
                break;
            default:
                break;
        }
    }

    public boolean changeNextItem() {
            try {
                items.get(curSelectIndex).setScaleX(1f);
                items.get(curSelectIndex).setScaleY(1f);
                curSelectIndex = (curSelectIndex + 1) % items.size();
                items.get(curSelectIndex).setScaleX(1.5f);
                items.get(curSelectIndex).setScaleY(1.5f);
                if(observer!=null)
                    observer.onChange(1);
            } catch (Exception e) {
                return false;
            }
            return true;
    }

    public boolean changeLastItem(){
        try {
            items.get(curSelectIndex).setScaleX(1f);
            items.get(curSelectIndex).setScaleY(1f);
            curSelectIndex = (curSelectIndex - 1)<0?items.size()-1:curSelectIndex-1;
            items.get(curSelectIndex).setScaleX(1.5f);
            items.get(curSelectIndex).setScaleY(1.5f);
            if(observer!=null)
                observer.onChange(-1);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public void setObserver(ChangeItemObserver observer){
        this.observer=observer;
    }
}
